package com.wwzh.interview.study.thread;

import lombok.Getter;

import java.util.concurrent.CountDownLatch;

enum CountryEnum{
    ONE(1,"齐"),
    TWO(2,"楚"),
    THREE(3,"燕"),
    FOUR(4,"赵"),
    FIVE(5,"韩"),
    SIX(6,"魏");

    @Getter private Integer retCode;
    @Getter private String retMsg;

    CountryEnum(Integer retCode, String retMsg) {
        this.retCode = retCode;
        this.retMsg = retMsg;
    }

    public static CountryEnum foreach_enum(int index){
        CountryEnum[] values = CountryEnum.values();
        for (CountryEnum ele:values
             ) {
            if (index == ele.getRetCode()){
                return ele;
            }
        }
        return null;
    }
}

public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(6);
        for (int i = 1; i <= 6; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName() +"国，被灭了");
                countDownLatch.countDown();
            },CountryEnum.foreach_enum(i).getRetMsg()).start();
        }
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName() +"秦国一统天下");
    }

    private static void closeDoor() throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                System.out.println(Thread.currentThread().getName() +"走人了");
                countDownLatch.countDown();
            },String.valueOf(i)).start();
        }
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName() +"班长最后关门走人了");
    }
}
